﻿/// <reference path="../Scripts/jquery-1.8.1-vsdoc.js" />

$.ajaxSetup({
    url: "/Handler/Project.ashx?" + (new Date()).valueOf(),
    type: "POST",
    dataType: "text",
    global: false,
});

$(document).ready(function () {
    BindDepartment();
    BindProjectAward();
    BindProjectStatus();
    BindProjectType();
    $("#ParentType").change(function () {
        BindProjectChildType($("#ParentType").find('option:selected').val());
    });
    $("#btnNext").bind("click", Save);
    $("#btnDel").bind("click", Del);
    var ID = $("#MainContent_ID").val();
    if (!Util.isEmpty(ID)) {
        $("#btnDel").show();
        BindData(ID,$("#MainContent_Category").val());
    }
});

function BindDepartment()
{
        $.ajax({
            url: "/Handler/Department.ashx?" + (new Date()).valueOf(),
            async:false,
            contentType:'application/x-www-form-urlencoded;charset=gb2312',
            data: {action:"loadData"},
            success: function (data) {
            var ret=$.parseJSON(data).Departments;
            var selects= $("#Department");
            selects.empty();
            selects.append("<option value='-1'>-请选择-</option>"); 
            for (var i=0;i<ret.length;i++) 
            {
                selects.append("<option value='"+decodeURIComponent(ret[i].ID)+"'>"+decodeURIComponent(ret[i].Name)+"</option>"); 
            }
        }
    });
}

function BindProjectType()
{
       BindProjectParentType();
}

function BindProjectParentType()
{
        $.ajax({
            url: "/Handler/ProjectType.ashx?" + (new Date()).valueOf(),
            async:false,
            contentType:'application/x-www-form-urlencoded;charset=gb2312',
            data: {action:"loadParentData"},
            success: function (data) {
            var ret=$.parseJSON(data).ProjectTypes;
            var selects= $("#ParentType");
            selects.empty();
            selects.append("<option value='-1'>-请选择-</option>"); 
            for (var i=0;i<ret.length;i++) 
            {
                selects.append("<option value='"+decodeURIComponent(ret[i].ID)+"'>"+decodeURIComponent(ret[i].Name)+"</option>"); 
            }
        }
    });
}

function BindProjectChildType(parentTypeID)
{
        $.ajax({
            url: "/Handler/ProjectType.ashx?" + (new Date()).valueOf(),
            async:false,
            contentType:'application/x-www-form-urlencoded;charset=gb2312',
            data: {action:"loadChildData",id:parentTypeID},
            success: function (data) {
            var ret=$.parseJSON(data).ProjectTypes;
            var selects= $("#ChildType");
            selects.empty();
            selects.append("<option value='-1'>-请选择-</option>"); 
            for (var i=0;i<ret.length;i++) 
            {
                selects.append("<option value='"+decodeURIComponent(ret[i].ID)+"'>"+decodeURIComponent(ret[i].Name)+"</option>"); 
            }
        }
    });
}

function BindProjectStatus()
{
        $.ajax({
            url: "/Handler/ProjectStatus.ashx?" + (new Date()).valueOf(),
            async:false,
            contentType:'application/x-www-form-urlencoded;charset=gb2312',
            data: {action:"loadData"},
            success: function (data) {
            var ret=$.parseJSON(data).ProjectStatus;
            var selects= $("#Status");
            selects.empty();
            selects.append("<option value='-1'>-请选择-</option>"); 
            for (var i=0;i<ret.length;i++) 
            {
                selects.append("<option value='"+decodeURIComponent(ret[i].ID)+"'>"+decodeURIComponent(ret[i].Name)+"</option>"); 
            }
        }
    });
}

function BindProjectAward()
{
        $.ajax({
            url: "/Handler/ProjectAward.ashx?" + (new Date()).valueOf(),
            contentType:'application/x-www-form-urlencoded;charset=gb2312',
            data: {action:"loadData"},
            async:false,
            success: function (data) {
            var ret=$.parseJSON(data).ProjectAwards;
            var selects= $("#Award");
            selects.empty();
            selects.append("<option value='-1'>-请选择-</option>"); 
            for (var i=0;i<ret.length;i++) 
            {
                selects.append("<option value='"+decodeURIComponent(ret[i].ID)+"'>"+decodeURIComponent(ret[i].Name)+"</option>"); 
            }
        }
    });
}

function Save() {    
    if(!IsValidate())
    {
        return;
    }
    var ret=GetData();
    var jsonStr=$.toJSON(ret);
    $.ajax({
        contentType:'application/x-www-form-urlencoded;charset=gb2312',
        data: {action:Util.isEmpty(ret.ID)?"addData":"updateData",jsonDataString:jsonStr},
        beforeSend: function () {
            Util.blockUI("正在保存数据...");
        },
        success: function (data) {
            Util.unblockUI();
            var obj=$.parseJSON(data);
            Util.freshPage("ProjectDetail.aspx?ID="+obj.MSG+"&Category="+$("#MainContent_Category").val());
        }
    });
}

function Del()
{
    if(!confirm("确定删除?"))
    {
       return;
    }

    var ret=
    {
       "ID":encodeURIComponent($("#MainContent_ID").val())
    }
    var jsonStr=$.toJSON(ret);
    $.ajax({
        contentType:'application/x-www-form-urlencoded;charset=gb2312',
        data: {action:"removeData",jsonDataString:jsonStr},
        beforeSend: function () {
            Util.blockUI("正在删除数据...");
        },
        success: function (data) {
            Util.unblockUI();
            Util.freshPage("ProjectList.aspx?Category="+$("#MainContent_Category").val());
        }
    });
}

function GetData() {
    var ret=
    {
       "ID":encodeURIComponent($("#MainContent_ID").val()),
       "ProjectID":encodeURIComponent($('#ProjectID').val()),   
       "OldID":encodeURIComponent($('#OldID').val()),
       "Name": encodeURIComponent($("#Name").val()),
       "Category": encodeURIComponent($("#MainContent_Category").val()),
       "StartDate": encodeURIComponent($("#StartDate").val()),
       "EndDate": encodeURIComponent($("#EndDate").val()),
       "DepID":encodeURIComponent($("#Department").find("option:selected").val()),
       "ParentTypeID": encodeURIComponent($("#ParentType").find("option:selected").val()),
       "ChildTypeID": encodeURIComponent($("#ChildType").find("option:selected").val()),
       "TypeDesc": encodeURIComponent($("#TypeDesc").val()),
       "TotalFund": encodeURIComponent($("#TotalFund").val()),
       "OutFund": encodeURIComponent($("#OutFund").val()),
       "PrjContent": encodeURIComponent($("#PrjContent").val()),
       "CheckIndex": encodeURIComponent($("#CheckIndex").val()),
       "StatusID": encodeURIComponent($("#Status").find("option:selected").val()),
       "EvaluateDate": encodeURIComponent($("#EvaluateDate").val()),
       "AwardID": encodeURIComponent($("#Award").find("option:selected").val()),
       "AwardDesc":encodeURIComponent($("#AwardDesc").val()),
       "Apply": encodeURIComponent($("#Apply").val()),
       "Memo": encodeURIComponent($("#Memo").val()),
       "LastModifiedBy":encodeURIComponent($("#MainContent_UserID").val())
    }

    return ret;
}

function IsValidate() {
    var msg="";
    if (Util.isEmpty($('#Name').val())) {
        msg="项目名称不能为空!";
    }
    else if (Util.isEmpty($('#ProjectID').val())) {
        msg="项目编号不能为空!";
    }
    else if (Util.isEmpty($('#StartDate').val())) {
        msg="项目开始时间不能为空!";
    }
    else if (Util.isEmpty($('#EndDate').val())) {
        msg="项目结束时间不能为空";
    }
    else if ($('#Department').val()=='-1') {
        msg="请选择项目所属部门!";
    }
    else if ($('#Type').val()=='-1') {
        return "请选择项目类别!";
    }
    else if (isNaN(parseFloat($('#TotalFund').val()))) {
        msg="项目总经费填写错误!";
    }
    else if (isNaN(parseFloat($('#OutFund').val()))) {
        msg="项目外来经费填写错误!";
    }
    else if ($('#AwardType').val()=='-1') {
        msg="请选择获奖情况!";
    }
    else if(compareDateTime($('#StartDate').val(),$('#EndDate').val())>0)
    {
        msg="项目开始时间不能晚于项目结束时间!";
    }

    if(Util.isEmpty(msg))
    {
        return true;
    }
    else
    {
        alert(msg);
        return false;
    }
}

function BindData(ID,Category) {
    $.ajax({
            contentType:'application/x-www-form-urlencoded;charset=gb2312',
            data: {action:"loadData",id:ID,category:$("#MainContent_Category").val()},
            beforeSend: function () {
                Util.blockUI("正在加载数据...");
            },
            success: function (data) {
                Util.unblockUI();
                var obj=$.parseJSON(data).Projects[0];
                $("#MainContent_ID").val(decodeURIComponent(obj.ID));
                $("#ProjectID").val(decodeURIComponent(obj.ProjectID));
                $("#OldID").val(decodeURIComponent(obj.OldID));
                $("#Name").val(decodeURIComponent(obj.Name));
                $("#StartDate").val(Util.changeDateFormat(decodeURIComponent(obj.StartDate)));
                $("#EndDate").val(Util.changeDateFormat(decodeURIComponent(obj.EndDate)));
                SelectedValue("Department",decodeURIComponent(obj.DepID));
                SelectedValue("ParentType",decodeURIComponent(obj.ParentTypeID));
                BindProjectChildType(decodeURIComponent(obj.ParentTypeID));
                SelectedValue("ChildType",decodeURIComponent(obj.ChildTypeID));
                $("#TypeDesc").val(decodeURIComponent(obj.TypeDesc));
                $("#TotalFund").val(obj.TotalFund);
                $("#OutFund").val(obj.OutFund);
                $("#PrjContent").val(decodeURIComponent(obj.PrjContent));
                $("#CheckIndex").val(decodeURIComponent(obj.CheckIndex));
                SelectedValue("Status",decodeURIComponent(obj.StatusID));
                $("#EvaluateDate").val(Util.changeDateFormat(decodeURIComponent(obj.EvaluateDate)));
                SelectedValue("Award",decodeURIComponent(obj.AwardID));
                $("#AwardDesc").val(decodeURIComponent(obj.AwardDesc));
                $("#Apply").val(decodeURIComponent(obj.Apply));
                $("#Memo").val(decodeURIComponent(obj.Memo));
            }
        });
}